#coding:utf-8
#-*- coding: UTF-8 -*-
##区服选择
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import os
import subprocess

from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage  as mi

directory       = sys.argv[0]
deviceName      = sys.argv[1]
deviceSerialNo  = sys.argv[2]
print(directory,deviceName,deviceSerialNo)

#连接设备(等待时间，‘版本号’)
device = mr.waitForConnection(10,deviceSerialNo)

try:
        device.wake()
except java.lang.NullPointerException,e:
        print >> sys.stderr, "%s: ERROR: Couldn't connect to %s: %s" % (progname, serialno, e)
        sys.exit(3)
#image = device.takeSnapshot()
#subImage= image.getSubImage((470,309,108,30))
#subImage.writeToFile('D:\\python_file\\monkeyrunner\\test4.png','png')


#获取任务
taskPyFile='%s\\getTask.py'%directory
index=0;
while True:
    taskProcess=subprocess.Popen(['python',taskPyFile,deviceName],shell=True,stdout=subprocess.PIPE)
    taskProcess.wait()
    result=taskProcess.stdout.read().strip('\n').strip('\r')
    taskId,server,account,password,role,area,rechargeType,money=result.split('~')

    print(taskId,server,account,password,role,area,rechargeType,money)


    #选择区服截图
    image = device.takeSnapshot()
    subImage= image.getSubImage((470,309,108,30))
    serverImagePath="%s\\image\\server\\server_%s_%s.png"%(directory,deviceName,taskId)
    print(serverImagePath)
    subImage.writeToFile(serverImagePath,'png')
    
    mr.sleep(10)
    index = index + 1
    print(index)

'''
#选择区服截图
#image = device.takeSnapshot()
#subImage= image.getSubImage((470,309,108,30))
serverImagePath='%s\\image\\server\\server_%s_%s.png'%(directory,deviceName,taskId)
#subImage.writeToFile(serverImagePath,'png')

ocrServerPyFile='%s\\ocrServer.py'%directory
selectServerProcess=subprocess.Popen(['python',ocrServerPyFile,serverImagePath,server],shell=True,stdout=subprocess.PIPE)
result=selectServerProcess.stdout.read().strip('\n').strip('\r')

##选区结果处理




#充值后游戏币数量截图
#image = device.takeSnapshot()
#subImage= image.getSubImage((470,309,108,30))
gameMoneyImagePath='%s\\image\\money\\money_%s_%s.png'%(directory,deviceName,taskId)
#subImage.writeToFile(gameMoneyImagePath,'png')

ocrMoneyPyFile='%s\\ocrMoney.py'%directory
ocrMoneyProcess=subprocess.Popen(['python',ocrMoneyPyFile,serverImagePath,taskId,money],shell=True,stdout=subprocess.PIPE)
result=ocrMoneyProcess.stdout.read().strip('\n').strip('\r')



'''



'''
if a!=server:
    print('not find')
else:
    print('find the server')
'''

#字符串编码问题
#判断循环
'''
while a!='王耆96区':
    device.touch(645,465,'DOWN_AND_UP')
    device.drag((645,465),(645,400),1)
    MonkeyRunner.sleep(0.2)
    result = device.takeSnapshot()
    result= result.getSubImage((470,309,108,30))
    result.writeToFile('D:\\python_file\\monkeyrunner\\test4.png','png')
    code1=os.popen("python D:\python_file\ocr_test\png.py")
    print(code1.read())
elif a=='王耆96区':
    print('ok2')
else :
    print(error)

'''


'''

while 1:
    if a.encode('utf-8')!='王耆96区':
        print(len(a.encode('utf-8')))
        print(len(a))
        print(len('王耆96区'))
        break
    else:
        device.touch(645,465,'DOWN_AND_UP')
        device.drag((645,465),(645,400),1)
        MonkeyRunner.sleep(0.2)
        result = device.takeSnapshot()
        result= result.getSubImage((470,309,108,30))
        result.writeToFile('D:\\python_file\\monkeyrunner\\test4.png','png')
        code1=os.popen("python D:\python_file\ocr_test\png.py")
        print(code1.read())
'''















'''

#第一个截图
pic=device.takeSnapshot()
#获取指定区域的图像(0,57,715,1123)，注意两个括号
pic= pic.getSubImage((0,57,715,1123))
pic.writeToFile('D:\\python_file\\monkeyrunner\\test.png','png')


#等待时间
print("Wait 2s")
logFile.write("Wait 2s")
MonkeyRunner.sleep(2)

#滑动屏幕;
for i in range(1,70):
    device.drag((250,850),(250,110),0.1,10)
#开始，结束，持续时间，步骤  
    device.drag((250,110),(250,850),0.1,10)
MonkeyRunner.sleep(1)

#触击屏幕;
device.touch(507,72,"DOWN_AND_UP")



'''
